<script lang="tsx">
/**
 * 字段配置
 */
export default {
  name: "field-form"
}
</script>

<script lang="tsx" setup>
import {nextTick, ref} from "vue";
import {defineDialogForm, definePageModel, DialogForm, PageModel, processRequest} from "@/framework";
import APIS from "@/apis";
import {ElMessage, ElMessageBox} from "element-plus";

/**
 * 菜单数据
 */
const menuId = ref()

/**
 * 弹窗实例
 */
const dialogFormModelRef = ref()
const form = defineDialogForm({
  width: "1000px",
  title: `菜单字段配置`
})

/**
 * 页面实例
 */
const pageModelRef = ref()
const config = definePageModel<any>({
  url: APIS.MENU_FIELD_BASE,
  getImmediate: false,
  otherParams: {
    menuId: null
  },
  searchForm: {
    els: [
      {
        eType: "el-input",
        prop: "keyword",
        props: {
          placeholder: "关键字",
          clearable: true
        }
      }
    ]
  },
  programForm: {
    els: [
      {
        label: "新增",
        eType: "el-button",
        props: {
          type: "primary"
        },
        event: "add"
      },
      {
        label: "删除",
        eType: "el-button",
        props: {
          type: "danger"
        },
        event: "delete-multi"
      }
    ]
  },
  table: {
    page: {
      enable: true,
      pageSize: 20
    },
    selection: true,
    operation: {
      width: 100,
      els: [
        {
          eType: "el-button",
          label: "编辑",
          event: "edit",
          props: {
            type: "primary",
            link: true
          }
        },
        {
          eType: "el-button",
          label: "删除",
          event: "delete",
          props: {
            type: "primary",
            link: true
          }
        },
      ]
    },
    props: {
      stripe: true,
      border: true
    },
    els: [
      {
        label: "字段key",
        prop: "key",
        minWidth: 120
      },
      {
        label: "字段名称",
        prop: "name",
        minWidth: 120
      },
      {
        label: "字段备注",
        prop: "remark",
        minWidth: 200
      },
    ]
  },
  form: {
    title: "新增/编辑",
    width: 500,
    sheetForm: {
      labelWidth: "100px",
      required: ["key", "name"],
      bindData(formData) {
        formData.menuId = menuId.value
        return formData
      },
      els: [
        {
          label: "字段key",
          prop: "key",
          eType: "el-input",
          props: {
            placeholder: "字段key",
            clearable: true
          }
        },
        {
          label: "字段名称",
          prop: "name",
          eType: "el-input",
          props: {
            placeholder: "字段名称",
            clearable: true
          }
        },
        {
          label: "字段备注",
          prop: "remark",
          eType: "el-input",
          props: {
            placeholder: "字段备注",
            type: "textarea",
            rows: 3
          }
        },
      ]
    }
  }
})

/**
 * 刷新
 */
const refreshTableData = (isClear: boolean = false) => {
  pageModelRef.value.refreshTableData()
  isClear && pageModelRef.value.clearSelection()
}

/**
 * 新增
 */
const handleAdd = () => {
  pageModelRef.value.handleAdd()
}

/**
 * 编辑
 */
const handleEdit = (row: any) => {
  pageModelRef.value.handleEdit(row)
}

/**
 * 删除
 */
const handleDelete = (row: any) => {
  pageModelRef.value.handleDelete(row)
}

/**
 * 刷新表格
 */
const handleRefresh = () => {
  pageModelRef.value.refreshTableData()
}

/**
 * 批量删除
 */
const handleDeleteMulti = async () => {
  const selectData = pageModelRef.value.getTableSelectData()
  if (!selectData) {
    ElMessage.warning("请先选择一条数据")
    return false
  }
  await ElMessageBox.confirm("是否确认删除?", "删除", {
    type: "error"
  })
  await processRequest.delete('/api/MenuField/DeleteMulti', {
    ids: selectData.map((item: any) => item.id)
  })

  ElMessage.success("删除成功")
  handleRefresh()
}

/**
 * 显示
 */
const show = (params: any) => {
  dialogFormModelRef.value.show()
  menuId.value = params.menuId
  if (config.otherParams) {
    config.otherParams.menuId = params.menuId
  }
  nextTick(() => {
    refreshTableData(true)
  })
}

defineExpose({
  show
})
</script>

<template>
  <DialogForm ref="dialogFormModelRef" :dialogFormModel="form">
    <div class="filed-form">
      <PageModel ref="pageModelRef" :PageModel="config" @add="handleAdd" @delete="handleDelete" @edit="handleEdit"
                 @delete-multi="handleDeleteMulti"/>
    </div>
  </DialogForm>
</template>

<style scoped lang="scss">
.filed-form {
  height: 600px;
  display: flex;
  flex-direction: column;
}
</style>